package com.ele.Servlet;

import com.ele.Pojo.PageResult;
import com.ele.Pojo.Shop;
import com.ele.Pojo.ShopCategory;
import com.ele.Service.ShopCategoryService;
import com.ele.Service.impl.ShopCategoryServiceImpl;
import com.google.gson.Gson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/shopCategory")
public class ShopCategoryServlet extends HttpServlet {
    //service
    private ShopCategoryService shopCategoryService = new ShopCategoryServiceImpl();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //查询和删除是get方法
        String op =req.getParameter("op");
        switch (op){
            case "find":
                findShopCategory(req,resp);break;
            case "delete":
                deleteShopCategory(req,resp);break;
        }

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        //修改和添加是post方法
        String op =req.getParameter("op");
        switch (op){
            case "add": addShopCategory(req,resp) ; break;
            case "update":
                updateShopCategory(req,resp);break;

        }

    }

    //添加店铺类型
    private void addShopCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        String categoryName = req.getParameter("categoryName");
        // 需要对数据进行校验
        // 调用service
        Integer x = shopCategoryService.shopCategoryAdd(categoryName );
        resp.getWriter().write(""+x);
    }
    //查询商店类型
    private void findShopCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String currentPage = req.getParameter("currentPage");
        String pageSize = req.getParameter("pageSize");
        PageResult<List<ShopCategory>> pageResult = shopCategoryService.findShopCategory(currentPage, pageSize);
        // 转成json
        String json = new Gson().toJson(pageResult);
        resp.setContentType("application/json;charset=utf-8");
        resp.getWriter().write(json);
    }

    /**
     * 删除商店类型
     * @param req
     * @param resp
     * @throws IOException
     */
    private void deleteShopCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String categoryId = req.getParameter("categoryId");
        Integer x = shopCategoryService.shopCategoryDelete(categoryId);
        resp.getWriter().write(""+x);
    }

    /**
     * 修改店铺类型
     * @param req
     * @param resp
     * @throws IOException
     */
    private void updateShopCategory(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String categoryName = req.getParameter("categoryName");
        String shopCategoryId = req.getParameter("shopCategoryId");

        Integer x = this.shopCategoryService.shopCategoryUpdate(categoryName, shopCategoryId);
        resp.getWriter().write(""+x);
    }

}
